<?php

class FieldAPIController extends \BaseController {

    /**
	 * Controller constructor
	 */
	public function __construct() {
		
	}
	
	/**
	 * Display a listing of fields in JSON format.
	 *
	 * @return JSON Response
	 */
	public function index()
	{
		return Response::json(Field::all());
	}


	/**
	 * Store a newly created field in storage from JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function store()
	{
		$newField = Input::json();
		
		$field = new Field();
		$field->name = $newField->get('name');
		$field->address = $newField->get('address');
		$field->town = $newField->get('town');
		$field->state = $newField->get('state');
		$field->country = $newField->get('country');
		$field->contact = "";
		$field->price = 0;
		$field->uploaded_by_user = 1;
		$field->save();
		
		return Response::json($field);
	}


	/**
	 * Display the specified field.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$field = Field::find($id);
		if(is_null($field)){
			return Response::json('Field not found', 404);
		} else {
			return Response::json($field);
		}
	}


	/**
	 * Update the specified field in storage from JSON put data.
	 *
	 * @return JSON Response
	 */
	public function update($id)
	{
		$field = Field::find($id);
		if(is_null($field)){
			return Response::json('Field not found', 404);
		}
		$field->name = $updateField->get('name');
		$field->address = $updateField->get('address');
		$field->town = $updateField->get('town');
		$field->state = $updateField->get('state');
		$field->country = $updateField->get('country');
		$field->contact = $updateField->get('contact');
		$field->price = $updateField->get('price') == "" ? 0 : $updateField->get('price');
		$field->save();
		
		return Response::json($field);
	}


	/**
	 * Remove the specified field from storage by posted id.
	 *
	 * @return JSON Response
	 */
	public function destroy($id)
	{
		$field = Field::find($id);
		
		if(is_null($field))
		{
			return Response::json('Field not found', 404);
		}
		$deletedField = $field;
		$field->delete();
		return Response::json($deletedField);
	}
}
